﻿//-----------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.ContactManager.WebSite
{
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using AutoMapper;
    using OverrideThis.ContactManager.WebSite.Infrastructure;
    using OverrideThis.ContactManager.WebSite.Infrastructure.Mapper;
    using OverrideThis.ContactManager.WebSite.Infrastructure.RouteConstraints;
    using OverrideThis.ContactManager.WebSite.Infrastructure.Validators;
    using OverrideThis.ContactManager.WebSite.Infrastructure.ViewEngines;

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "BirthDateRoute",
                "Person/BirthDate/{month}/{day}/{year}",
                new
                {
                    controller = "Person",
                    action = "Birthdate",
                    month = UrlParameter.Optional,
                    day = UrlParameter.Optional,
                    year = UrlParameter.Optional
                },
                new
                {
                    month = new DateRouteConstraint(DateRouteConstraintType.Month),
                    year = new DateRouteConstraint(DateRouteConstraintType.Year),
                    day = new DateRouteConstraint(DateRouteConstraintType.Day)
                }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
        
        protected void Application_Start()
        {
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CorporateEmailAttribute), typeof(CorporateEmailValidator));
            ControllerBuilder.Current.SetControllerFactory(typeof(StructureMapControllerFactory));
            ViewEngines.Engines.Add(new TokenBasedViewEngine());
            log4net.Config.XmlConfigurator.Configure();
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
            new BootStrapper().BootStrap("connKey");
            Mapper.Configuration.AddProfile<ContactManagerMapperProfile>();
        }
    }   
}